function hfile2 = vmr_LoResRescale(hfile, cutoff)
% VMR::LoResRescale  - bring 0.5mm hires back to VMR space
%
% FORMAT:       lores = vmr.LoResRescale([cutoff])
%
% Input fields:
%
%       cutoff      optional flag, minimize VMR box (default false)
%
% Output fields:
%
%       lores       1x1x1 mm VMR

% Version:  v0.7b
% Build:    7090217
% Date:     Sep-02 2007, 5:34 PM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 1 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'vmr')
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to ''%s''.', ...
        mfilename ...
    );
end
bc = bvqxfile_getcont(hfile.L);
if ~isfield(bc, 'VoxResX') || ...
    any([bc.VoxResX, bc.VoxResY, bc.VoxResZ] ~= 0.5)
    error( ...
        'BVQXfile:InvalidObject', ...
        'Method only valid for 0.5mm ISOvoxel VMRs.' ...
    );
end
if nargin < 2 || ...
   ~islogical(cutoff) || ...
    isempty(cutoff)
    cutoff = false;
else
    cutoff = cutoff(1);
end

% what outbox
if cutoff
    obox = [bc.OffsetX, bc.OffsetY, bc.OffsetZ];
    obox = [floor(obox / 2); floor((obox + size(bc.VMRData) - 1) / 2)];
else
    obox = [0, 0, 0; 255, 255, 255];
end

% make temporary copy
tfilel = [];
try
    tfile = aft_CopyObject(hfile);
    tfilel = tfile.L;
    vmr_Reframe(tfile, [obox(1, :) .* 2; obox(2, :) .* 2 + 1]);
    tfilec = bvqxfile_getcont(tfilel);
    bvqxfile_clear(tfilel);
catch
    if ~isempty(tfilel)
        bvqxfile_clear(tfilel);
    end
    error( ...
        'BVQXfile:OutOfMemory', ...
        'Error creating temporary sampling object.' ...
    );
end

% create output VMR
hfile2 = BVQXfile('new:vmr');
vmr_Reframe(hfile2, obox);
bc2 = bvqxfile_getcont(hfile2.L);
nd = uint16([]);
nd(1:(obox(2, 1) + 1), 1:(obox(2, 2) + 1), 1:(obox(2, 3) + 1)) = 0;

% sum elements
for x = 1:2
    for y = 1:2
        for z = 1:2
            nd = nd + ...
                uint16(tfilec.VMRData(x:2:end, y:2:end, z:2:end));
        end
    end
end
for zc = 1:size(nd, 3)
    nd(:, :, zc) = uint16(double(nd(:, :, zc)) ./ 8);
end
if strcmpi(class(tfilec.VMRData), 'uint8')
    nd = uint8(nd);
end
bc2.VMRData = nd;

% do the same for V16 data if present
if ~isempty(tfilec.VMRData16) && ...
    numel(size(tfilec.VMRData16)) == numel(size(tfilec.VMRData)) && ...
    all(size(tfilec.VMRData16) == size(tfilec.VMRData))
    nd = uint16([]);
    nd(1:(obox(2, 1) + 1), 1:(obox(2, 2) + 1), 1:(obox(2, 3) + 1)) = 0;
    for x = 1:2
        for y = 1:2
            for z = 1:2
                nd = nd + ...
                    tfilec.VMRData16(x:2:end, y:2:end, z:2:end);
            end
        end
    end
    for zc = 1:size(nd, 3)
        nd(:, :, zc) = uint16(double(nd(:, :, zc)) ./ 8);
    end
    bc2.VMRData16 = nd;
end

% set offsets / dims
bc2.OffSetX = obox(1, 1);
bc2.OffSetY = obox(1, 2);
bc2.OffSetZ = obox(1, 3);
bc2.DimX = size(nd, 1);
bc2.DimY = size(nd, 2);
bc2.DimZ = size(nd, 3);
bvqxfile_setcont(hfile2.L, bc2);
